import axios from 'axios'
import {
  getRedirectPath
} from '../util'
const AUTH_SUCCESS = 'AUTH_SUCCESS'
const ERROR_MSG = 'ERROR_MSG'
const LOAD_DATA = 'LOAD_DATA'
const LOGOUT = 'LOGOUT'

const initState = {
  redirectTo: '',
  msg: '',
  user: '',
  type: ''
}
// reducer
export function user(state = initState, action) {
  switch (action.type) {
    case AUTH_SUCCESS:
      return { ...state,
        msg: '',
        redirectTo: getRedirectPath(action.payload),
        isAuth: true,
        ...action.payload
      }
    case LOAD_DATA:
      return { ...state,
        ...action.payload
      }
    case ERROR_MSG:
      return { ...state,
        msg: action.msg
      }
    case LOGOUT:
      return {...initState, redirectTo: '/login'}
    default:
      return state
  }
}

function authSuccess(data) {
  return {
    type: AUTH_SUCCESS,
    payload: data
  }
}

function errorMsg(msg) {
  return {
    msg,
    type: ERROR_MSG
  }
}

export function loadData(userinfo) {
  return {
    type: LOAD_DATA,
    payload: userinfo
  }
}

// login
export function login({ user, pwd }) {
  if (!user || !pwd) {
    return errorMsg('用户密码必须输入')
  }
  return dispatch => {
    axios.post('/api/user/login', { user, pwd })
      .then(res => {
        if (res.status === 200 && res.data.code === 0) {
          // dispatch(registerSuccess({user,pwd,type}))
          dispatch(authSuccess(res.data.data))
        } else {
          dispatch(errorMsg(res.data.msg))
        }
      })
      .catch(err => {
        console.log(err)
        dispatch(errorMsg('登录失败'))
      })
  }
}

// register
export function register({ user, pwd, repeatpwd, type }) {
  if (!user || !pwd || !type) {
    return errorMsg('用户名密码必须输入')
  }
  if (pwd !== repeatpwd) {
    return errorMsg('密码和确认密码不同')
  }
  return dispatch => {
    axios.post('/api/user/register', { user, pwd, type })
      .then(res => {
        if (res.status === 200 && res.data.code === 0) {
          dispatch(authSuccess(res.data.data))
        } else {
          dispatch(errorMsg(res.data.msg))
        }
      })
      .catch(err => {
        console.log(err)
        dispatch(errorMsg('注册失败'))
      })
  }
}

export function update(data) {
  return dispatch => {
    axios.post('/api/user/update', data).then(res => {
      if (res.status === 200 && res.data.code === 0) {
        dispatch(authSuccess(res.data.data))
      } else {
        dispatch(errorMsg(res.data.msg))
      }
    }).catch(err => {
      console.log(err)
      dispatch(errorMsg('更新信息失败'))
    })
  }
}

export function logoutSubmit() {
  return {type: LOGOUT}
}